import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:happy_elf_app/apis/user.dart';
import 'package:happy_elf_app/store/config.dart';
import 'package:happy_elf_app/store/user.dart';
import 'package:happy_elf_app/utils/adaption.dart';
import 'package:happy_elf_app/utils/color.dart';
import 'package:happy_elf_app/utils/globalKey.dart';

class Login extends StatefulWidget {
  const Login({super.key});

  @override
  State<Login> createState() => _LoginState();
}

final UserDataProvider = Provider.of<UserData>(
  Global.navigatorKey.currentContext!,
  listen: false,
);

class _LoginState extends State<Login> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  TextEditingController accountController = TextEditingController(); // 账号
  TextEditingController passwordController = TextEditingController(); // 密码

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: hexColor('#F5F5F5'),
      padding: EdgeInsets.only(top: 320.px),
      child: Column(
        children: [
          Image.asset(
            'lib/assets/images/login/login-bg.png',
            width: 260,
            height: 150,
          ),
          // 账号输入框
          DecoratedBox(
            // 1. 绘制渐变色边框背景
            decoration: BoxDecoration(
              gradient: const LinearGradient(
                  colors: [Color(0xFFFFBEB5), Color(0xFFE5BCFF)]),
              borderRadius: BorderRadius.circular(32),
            ),
            child: Container(
              width: 275,
              height: 60.px,
              margin: const EdgeInsets.all(2.0), // 设置内边距，避免内容紧贴边框
              decoration: BoxDecoration(
                color: Colors.white, // 内部容器背景色
                borderRadius: BorderRadius.circular(32),
              ),
              child: Focus(
                onFocusChange: (hafocus) {
                  // 失去焦点
                  if (!hafocus) {
                    // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual);
                    // print('1${accountController.text}');
                  }
                },
                child: TextField(
                  controller: accountController,
                  decoration: InputDecoration(
                    border: InputBorder.none,
                    focusedBorder: InputBorder.none,
                    enabledBorder: InputBorder.none,
                    hintText: '请输入账号',
                    hintMaxLines: 1,
                    contentPadding: EdgeInsets.only(left: 10.px, bottom: 26.px),
                  ),
                  style: TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ),
            ),
          ),
          SizedBox(height: 20),
          // 密码
          DecoratedBox(
            // 1. 绘制渐变色边框背景
            decoration: BoxDecoration(
              gradient: const LinearGradient(
                  colors: [Color(0xFFFFBEB5), Color(0xFFE5BCFF)]),
              borderRadius: BorderRadius.circular(32),
            ),
            child: Container(
              width: 275,
              height: 60.px,
              margin: const EdgeInsets.all(2.0), // 设置内边距，避免内容紧贴边框
              decoration: BoxDecoration(
                color: Colors.white, // 内部容器背景色
                borderRadius: BorderRadius.circular(32),
              ),
              child: Focus(
                onFocusChange: (hafocus) {
                  // 失去焦点
                  if (!hafocus) {
                    // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual);
                    print('1${passwordController.text}');
                  }
                },
                child: TextField(
                  controller: passwordController,
                  decoration: InputDecoration(
                    border: InputBorder.none,
                    focusedBorder: InputBorder.none,
                    enabledBorder: InputBorder.none,
                    hintText: '请输入密码',
                    hintMaxLines: 1,
                    contentPadding: EdgeInsets.only(left: 10.px, bottom: 26.px),
                  ),
                  style: TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ),
            ),
          ),
          GestureDetector(
            onTap: () async {
              try {
                print('11111--${accountController.text}');
                print('11111--${passwordController.text}');
                var res = await User().login(data: {
                  'username': accountController.text,
                  'password': passwordController.text
                });
                print('111---$res');
                if (res['code'] == 200) {
                  var result = await User().getUserInfo();
                  print('用户信息$result');
                  if (result['code'] == 200) {
                    UserDataProvider.setUserData(result['data']);
                    // 存储用户信息到本地或全局状态管理
                    print('用户信息保存成功');
                    if (result['data']['isFirstLogin']) {
                      // 第一次登陆去介绍页面
                      Navigator.pushNamed(context, 'Introduction');
                    } else {
                      Navigator.pushNamed(context, 'MainPage');
                    }
                  }

                  print('登录成功');
                  FocusScope.of(context).unfocus();
                }
              } catch (e) {
                print('登录异常$e');
              }
            },
            child: Container(
              width: 120,
              height: 80.px,
              margin: EdgeInsets.only(top: 80.px),
              decoration: BoxDecoration(
                gradient: const LinearGradient(
                    colors: [Color(0xFFFFBEB5), Color(0xFFE5BCFF)]),
                borderRadius: BorderRadius.circular(18),
              ),
              child: Center(
                child: Text(
                  '登录',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 32.px,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}
